# The planner's problem with infinite horizon and transition path
# Without financial frictions

##################DeClaration of Parameters / Variables ###################
# The number of periods the economy converges to the unconstrained steady state
# Since T is large (>=100), the economy converges before reaching T

param T; 
param shock_start;
param shock_duration;

#-- Households' parameters
param BETA;   # discount factor
param SIGMA;  # risk-aversion parameter
param CHI;    # entrepreneur share
param NU;     # labor supply elasticity
param MU;     # disutility of labor supply

#-- Firms' parameters
param A;      # aggregate productivity
param ALPHA;  # capital share
param DELTA;  # depreciation rate, may be set to 1 in the simple scenarios

#-- Financial market parameters
param eta_0;  # constant term in the cost function for matching
param eta_1;  # curvature in the cost function for matching
   
#-- Gov' Parameters
param G;      # government spending in steady state

#-- End point (solved from steady state, but not used)
param K_SS;
param B_SS;
param C_SS;
param L_SS;
param phi_SS;
param eta_SS;
param rho_SS;
param q_SS;
param q_w_SS;

#-- Initial point
param K_ini;
param B_ini;
param tau_k_0;

#-- Government spending shock
param fiscal_shock;    # if it is 1, then run fiscal shock; see the .run file
param gov_spending_up; # the magnitude of fiscal shock

#---------------Declaration of Variables (only allocations)----------------
# planner's allocation 
var K {t in -1..T}   >= 0;
var B {t in -1..T} ;
var C {t in 0..T}    >= 0;
var L {t in 0..T}    >= 0;
var phi {t in 0..T}  >= 0, <=1;
var I {t in 0..T}; # investment

# asset market features
var q_w {t in 0..T} >= 1;
var q {t in 0..T}   >= 1;
var eta {t in 0..T} >= 0;
var rho {t in 0..T} >= 0;
var eta_p {t in 0..T};

# policy impementation
var R{t in 0..T};
var tau_k{t in 0..T};
var tau_l{t in 0..T};

# exogenous given variables (note: if we use parameters, we cannot vary them)
var Gov_spending{t in 0..T};

#################### Maximize the objective: Welfare ######################

maximize social_welfare:
(sum{t in 0..T-1} BETA^t * (log(C[t]) - MU * L[t]^(1 + NU) / (1 + NU)))
+ BETA^T / (1 - BETA) * (log(C[T]) - MU * L[T]^(1 + NU) / (1 + NU));


#################### The Planner's Constraints ############################

#------------------------- Social Resources Constraints
subject to soc {t in 0..T}:
C[t] + Gov_spending[t] + (1 + phi[t] * eta[t]) * K[t]
= A * K[t-1]^ALPHA * L[t]^(1 - ALPHA) + (1 + (1 - CHI) * phi[t] * eta[t]) * (1 - DELTA) * K[t-1];


#------------------------- Implementatbility Constraints:
# Implementability constraints by repeatedly using eq (42) in the paper
# Notice that we do the above for all t = 0, 1, 2,..., T
# There is another one for T because we assume that is the steady state 

/*
#### Flow implementability constraint which gives the same result
#### You can use this set of constraints instead of the set after with the
#### the same constraint names.

subject to implementability_at_0:
C[0]^(-SIGMA) * C[0] - MU * L[0]^NU * L[0] + C[0]^(-SIGMA) * B[0] + C[0]^(-SIGMA) * K[0]
= C[0]^(-SIGMA) * 
(B[-1] 
+ (1 - tau_k[0]) * A * ALPHA * K[-1]^(ALPHA - 1) * L[0]^(1 - ALPHA) * K[-1]
+ (1 - DELTA) * K[-1]
);

subject to implementability{t in 1..T}:
C[t]^(-SIGMA) * C[t] - MU * L[t]^NU * L[t]  + C[t]^(-SIGMA) * B[t] + C[t]^(-SIGMA) * K[t] 
=  C[t-1]^(-SIGMA) * B[t-1] / BETA + C[t-1]^(-SIGMA) * K[t-1]/ BETA;
*/

subject to implementability_at_0:
sum{s in 0..T-1} BETA^s * (C[s]^(-SIGMA) * C[s] - MU * L[s]^NU * L[s])
+ BETA^T / (1 - BETA) * (C[T]^(-SIGMA) * C[T] - MU * L[T]^NU * L[T] )
= C[0]^(-SIGMA) *
(B[-1]  
+ (1 - tau_k[0]) * A * ALPHA * K[-1]^ALPHA * L[0]^(1 - ALPHA) * K[-1]
+ tau_k[0] * DELTA * K[-1] 
+ (1 - DELTA) * K[-1]
);

subject to implementability{t in 1..T}:
sum{s in 0..T-t-1} BETA^s * (C[t+s]^(-SIGMA) * C[t+s] - MU * L[t+s]^NU * L[t+s])
+ BETA^(T-t) / (1 - BETA) * (C[T]^(-SIGMA) * C[T] - MU * L[T]^NU * L[T] )
= C[t-1]^(-SIGMA) *(B[t-1] + K[t-1]) / BETA;


subject to implementability_final_steady_state:
(C[T]^(-SIGMA) * C[T] - MU * L[T]^NU * L[T] ) / (1 - BETA)
= C[T]^(-SIGMA) *(B[T] + K[T]) / BETA;


#----------------------- Definitions (directly copied from cases with phi >=0, not really useful but does not matter)

# Note: There is no financing constraint; Financing constraint is not an issue; 
# Also, make phi = 0 so that q = 1 as if we are in a neo-classical model
# But, really it does not matter because we have already make q = q_w = 1 
# in above constraints
subject to ramsey_standard_case {t in 0..T}:
phi[t] = 0;

subject to def_eta {t in 0..T}:
eta[t] = eta_0 * phi[t]^eta_1;

subject to def_eta_p {t in 0..T}:
eta_p[t] = eta_0 * eta_1 * phi[t]^(eta_1 - 1);

subject to def_q {t in 0..T}:
q_w[t] = 1 + eta[t] + (1 - phi[t]) * phi[t] * eta_p[t];

subject to def_q_w {t in 0..T}:
q[t] = q_w[t] - eta[t];

subject to def_rho {t in 0..T}:
rho[t] * (1 - phi[t]^2 * eta_p[t]) = eta[t] + phi[t] * eta_p[t];


#------------------------ Taxes and interest rates
subject to investment{t in 0..T}:
I[t] = K[t] - (1 - DELTA) * K[t-1];

subject to interest_rate{t in 0..T-1}:
1 = BETA * C[t+1]^(-SIGMA) / C[t]^(-SIGMA) * R[t+1] * (1 + CHI * rho[t+1]);

subject to capital_taxes{t in 0..T-1}:
q_w[t] = BETA * C[t+1]^(-SIGMA) / C[t]^(-SIGMA) * ((1 - tau_k[t+1]) * ALPHA * K[t]^(ALPHA - 1) * L[t+1]^(1 - ALPHA) 
+ (1 - DELTA * (1 - tau_k[t+1])) * q_w[t+1] + CHI * (1 - DELTA) * phi[t+1] * (q[t+1] * (1 + rho[t+1]) - q_w[t+1]));

subject to labor_taxes{t in 0..T}:
(1 - tau_l[t]) * (1 - ALPHA) * K[t-1]^ALPHA * L[t]^(-ALPHA) * C[t]^(-SIGMA) = MU * L[t]^NU;


#####################     Initial point    ################################

subject to initial_capital_tax:
tau_k[0] = tau_k_0;

subject to initial_rate:
R[0] = 1;

subject to ini_point_K:
K[-1] = K_ini;

subject to ini_point_B:
B[-1] = B_ini;



########################    Shocks    #####################################

# The constraints when there are no fiscal shocks
subject to no_fiscal_shock{t in 0..T}:
Gov_spending[t] = G;

# The constraints when there are fiscal shocks
subject to fiscal_shock_1 {t in shock_start..shock_start + shock_duration - 1}:
Gov_spending[t] = G * (1 + gov_spending_up);

subject to fiscal_shock_2{t in 0..shock_start - 1 union shock_start + shock_duration..T}:
Gov_spending[t] = G;





